<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="addFeeRule">增加停车计费规则</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column label="序号" type="index" :index="indexMethod" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" prop="chargeType">
          <template #default="scope">
            {{formateChargeType(scope.row.chargeType)}}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pager" style="margin-top: 10px; float: right">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :pageSize="params.pageSize"
          @current-change="changePage"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 弹框 -->
     <AddRule :dialog-visible="dialogVisible"/>
  </div>
</template>

<script>
import AddRule from './components/AddRule.vue'
import { getRuleListAPI} from "@/api/carrule.js";
import { utils, writeFileXLSX } from "xlsx";
export default {
  name: "Building",
  components:{
    AddRule
  },
  data() {
    return {
      params: {
        page: 1,
        pageSize: 2,
      },
      ruleList: [],
      total: 0,
      dialogVisible:false,
    };
  },
  methods: {
    async getRuleList() {
      //获取计费规则列表
      const res = await getRuleListAPI(this.params);
      console.log(res, "getRuleList");
      this.ruleList = res.data.rows;
      this.total = res.data.total;
    },
    changePage(page) {
      //切换分页
      this.params.page = page;
      this.getRuleList();
    },
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1;
    },
    async exportExcel() {
      //导出表格
      const res = await getRuleListAPI(params);
      // 表头英文字段key
      const tableHeaderKeys = [
        "ruleNumber",
        "ruleName",
        "freeDuration",
        "chargeCeiling",
        "chargeType",
        "ruleNameView",
      ];
      // 表头中文字段value
      const tableHeaderValues = [
        "计费规则编号",
        "计费规则名称",
        "免费时长(分钟)",
        "收费上线(元)",
        "计费方式",
        "计费规则",
      ];
      //要求导出的字段
      const list =  res.data.rows.map(item=>{
        //创建一个新对象
        const obj = {}
        tableHeaderKeys.forEach(key=>
         obj[key] = item[key]
        )
        return obj
      })
      // 创建一个新的工作簿
      const workbook = utils.book_new();
      // 创建一个工作表 要求一个对象数组格式
      const worksheet = utils.json_to_sheet(list);

      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, "Data");
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: "A1" });
      // 导出方法进行导出
      writeFileXLSX(workbook, "SheetJSVueAoO.xlsx");
    },
    formateChargeType(chargeType){//格式化收费类型
      const Map = {
        duration:'时长收费',
        turn:'按次收费',
        partition:'分段收费'
      }
      return Map[chargeType]
    },
    addFeeRule(){//添加计费规则
      this.dialogVisible = true
    },
  },
  created() {
    this.getRuleList();
  },
};
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
</style>